package com.itheima.reggie.common;

import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLException;

@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(SQLException.class)
    public R<String> exception(SQLException sqlException){
        String message = sqlException.getMessage();
        if (message.contains("Duplicate entry"))
        {
            String s = message.split(" ")[2];
            return R.error(s+"用户名已存在");
        }
        if (message.contains("Data too long"))
        {
            String s = message.split(" ")[7];
            return R.error(s+"填写数过长");
        }
        return R.error("未知错误");
    }
    @ExceptionHandler(CustomerException.class)
    public R<String> exception2(CustomerException customerException){
        String message = customerException.getMessage();
        return R.error(message);
    }
}
